#!/usr/bin/env python
# -*- coding: utf-8 -*-

############################################################################
#    Copyright (C) 2008 by Daniele Costarella                              #
#    daniele.costarella@gmail.com                                          #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

import os, string

DATE = ""
NMBR = ""
TIME = "" 



# last 10 calls array
callsArray = []

# array pronto per la stampa in curses
tempArray = []
def adjustArray(callsArray):
   olddata = ""
   for item in callsArray:
      tempArray.append(adjustLine2(item))
   for item in tempArray:
      if item == None:
	 tempArray[tempArray.index(item)] = "---"
      elif string.split(item," ")[0] != olddata:
	 olddata = string.split(item, " ")[0]
      else:
	 olddata = string.split(item, " ")[0]
	 tempArray[tempArray.index(item)] = "     "+item[5:]
      

def adjustLine2(line):
   try:
      return string.split(string.split(line," ")[0],"-")[2] + "/"+string.split(string.split(line," ")[0],"-")[1] + " " + string.split(line," ")[1][:5] + " " + string.split(line," CALL ")[1]
   except IndexError: 
      return None   
   

def adjustLine(): # not used!
#2008-09-04
   i = 0
   try:
      for item in callsArray:
         line = callsArray[i]
         callsArray[i] = string.split(string.split(line[:-1]," ")[0],"-")[2] + "/"+string.split(string.split(line[:-1]," ")[0],"-")[1] + " " + string.split(line[:-1]," ")[1][:5] + " " + string.split(line[:-1]," CALL ")[1]
         i += 1
   except IndexError: pass

# speaker function
# enable the speaker to announce the calling incoming
def speaker(choice):
   matching = {"P": 'alsaplayer wave/papa.wav > /dev/null &',
               "M": 'alsaplayer wave/mamma.wav > /dev/null &',
	       "I": 'alsaplayer wave/non.wav > /dev/null &',
	       "L": 'alsaplayer wave/laura.wav > /dev/null &',
	       "D": 'alsaplayer wave/daniele.wav > /dev/null &',
	       "F": 'alsaplayer wave/mamma.wav > /dev/null &',
	       "G": 'alsaplayer wave/papa.wav > /dev/null &',
	       "X": 'alsaplayer wave/non.wav > /dev/null &'}
   #print matching["P"]
   os.system(matching[choice])
   
giorno = "Friday 26 September"
def data_it(day):
   #d = []
   import string
   d = string.split(day," ")
   giorni = {"Monday": "lunedi'", "Tuesday": "martedi'", "Wednesday": u"mercoledi'", "Thursday": u"giovedi'", "Friday": u"venerdi'", "Saturday": "sabato", "Sunday": "domenica"}
   mesi = {"January": "gennaio", "February": "febbraio", "March": "marzo", "April": "aprile", "May": "maggio", "June": "giugno", "July": "luglio", "August": "agosto", "September": "settembre" , "October": "ottobre", "November": "novembre", "December": "dicembre"}
   return giorni[string.split(day," ")[0]]+" "+string.split(day," ")[1]+" "+mesi[string.split(day," ")[2]]
  

### web extension functions ###

def check4update(): #return 'True' if a more updated version is available
   import urllib2
   wversion = ""
   try:
      wfile = urllib2.urlopen("http://danielecostarella.com/files/pyhome/issue")
      wversion = wfile.readline()
      wfile.close()
      lfile = open("issue")
      lversion = lfile.read()[:-1]
      lfile.close()
      if float(lversion) < float(wversion):
         os.system('aplay -q wave/Speech_On.wav&')
         return 1
      else:
         return 0
   except urllib2.URLError:
      return 0
      
def date(date):
   try:
      months = {"01": "Gennaio", "02": "Febbraio", "03": "Marzo", "04": "Aprile", "05": "Maggio", "06": "Giugno",
             "07": "Luglio", "08": "Agosto", "09": "Settembre", "10": "Ottobre", "11": "Novembre", "12": "Dicembre"}
      return date[2:] + " " + months[date[:2]]
   except KeyError:
      logger.warning('Data non valida')
      return "data non valida"
      
def cidtime(time):
   return time[:2] + ":" + time[2:]


if __name__ == "__main__":
  #speaker('G') # for tests
  if check4update():
     print "Nuova versione disponibile"
  else:
     print "Nessuna nuova versione disponibile"